package astar;


public class NodeList {
	
	public int size;
	private int areaWidth, areaHeight;
	private Node[][] nodePositionHash;
	
	public NodeList(int areaWidth, int areaHeight) {
		this.areaWidth = areaWidth;
		this.areaHeight = areaHeight;
		this.nodePositionHash = new Node[areaWidth][areaHeight];
	}
	
	public void add(Node n) {
		if(n!=null && this.nodePositionHash[n.x][n.y]==null) {
			this.size++;
		}
		this.nodePositionHash[n.x][n.y] = n;
	}
	
	public void remove(int x, int y){
		if(this.size==0 || x >= this.areaWidth || y >= this.areaHeight) {
			return;
		}
		Node existingN = this.nodePositionHash[x][y];
		if(existingN==null) {
			return;
		}
		this.nodePositionHash[x][y] = null;
		this.size--;
	}
	
	public Node getFirst(){
		if(this.size<=0) {
			return null;
		}
		int x, y;
		for(x=0; x<this.areaWidth; x++) {
			for(y=0; y<this.areaHeight; y++) {
				Node n = this.nodePositionHash[x][y];
				if(n!=null) {
					return n;
				}
			}
		}
		return null;
	}
	
	public Node searchByPosition(int x, int y){
		return this.nodePositionHash[x][y];
	}
	
	public Node getLowestFNode(){
		if(this.size == 0) {
			return null;
		}

		Node lowestFNode = null;
		int x, y;
		for(x=0; x<this.areaWidth; x++) {
			for(y=0; y<this.areaHeight; y++) {
				Node n = this.searchByPosition(x, y);
				if(n!=null) {
					if(lowestFNode==null) {
						lowestFNode=n;
					} else {
						if(n.getF() < lowestFNode.getF()) {
							lowestFNode = n;
						}
					}
				}
			}
		}

		return lowestFNode;
	}

}
